package org.dromara.common.sms.core.dao

import org.dromara.common.core.constant.GlobalConstants
import org.dromara.common.redis.utils.RedisUtils.deleteKeys
import org.dromara.common.redis.utils.RedisUtils.deleteObject
import org.dromara.common.redis.utils.RedisUtils.getCacheObject
import org.dromara.common.redis.utils.RedisUtils.setCacheObject
import org.dromara.sms4j.api.dao.SmsDao
import java.time.Duration

/**
 * SmsDao缓存配置 (使用框架自带RedisUtils实现 协议统一)
 * <p>主要用于短信重试和拦截的缓存
 *
 * @author Feng
 * @updater LikeYouDo
 * @date 2025/1/10 09:45
 */
open class PlusSmsDao : SmsDao {
    /**
     * 存储
     *
     * @param key       键
     * @param value     值
     * @param cacheTime 缓存时间（单位：秒)
     */
    override fun set(key: String, value: Any, cacheTime: Long) {
        setCacheObject(GlobalConstants.GLOBAL_REDIS_KEY + key, value, Duration.ofSeconds(cacheTime))
    }

    /**
     * 存储
     *
     * @param key   键
     * @param value 值
     */
    override fun set(key: String, value: Any) {
        setCacheObject(GlobalConstants.GLOBAL_REDIS_KEY + key, value, true)
    }

    /**
     * 读取
     *
     * @param key 键
     * @return 值
     */
    override fun get(key: String): Any? {
        return getCacheObject(GlobalConstants.GLOBAL_REDIS_KEY + key)
    }

    /**
     * remove
     *
     *  根据key移除缓存
     *
     * @param key 缓存键
     * @return 被删除的value
     * @author :Wind
     */
    override fun remove(key: String): Any {
        return deleteObject(GlobalConstants.GLOBAL_REDIS_KEY + key)
    }

    /**
     * 清空
     */
    override fun clean() {
        deleteKeys(GlobalConstants.GLOBAL_REDIS_KEY + "sms:*")
    }

}
